<template>
	<view class="home-box" v-if="isLoading">
		<!-- <image class="home-title" src="../static/image/home/home_title.png" mode=""></image> -->
		<view class="home-header">
			<view class="home-header-title1">
				临时用地复垦验收
			</view>
			<view class="home-header-title2">
				申请填报系统
			</view>
		</view>
		<view class="home-container">
			<view class="home-sub" @click="applyButtonClicked">
				<image class="home-sub-image" src="../static/image/home/home_approve.png" mode=""></image>
				<view class="home-sub-item">
					<view class="home-sub-item-title">
						在线申报
					</view>
					<text class="home-sub-item-button">
						立即进入
						<uni-icons type="right" color="#1E92F0" size="12"></uni-icons>
					</text>
				</view>
			</view>
			<view class="home-sub home-sub-status" @click="checkButtonClicked">
				<image class="home-sub-image" src="../static/image/home/home_status.png" mode=""></image>
				<view class="home-sub-item">
					<view class="home-sub-item-title">
						申报状态
					</view>
					<text class="home-sub-item-button">
						立即进入
						<uni-icons type="right" color="#1E92F0" size="12"></uni-icons>
					</text>
				</view>
			</view>
		</view>
		<view class="bottom-box">
			<view class="">
				本服务由酉阳土家族苗族自治县
				规划和自然资源局提供
			</view>

			<view class="" @click="callMe">
				服务咨询电话：<text style="color: #387ad4;">15923722588</text>
			</view>
		</view>

	</view>
</template>

<script>
	import request from '@/utils/request.js'
	export default {
		data() {
			return {
				jsapiToken: '',
				refreshToken: '',
				authCode: '',
				reqCode: '',
				userId: '',
				userType: '', //1：个人；3：法人
				userInfo: {},
				userCurrentQy: {},
				userAllQy: [],
				listImage: [],
				isLoading: false,
			};
		},
		onLoad() {
			uni.setStorageSync('appToken', '')
			this.getToken()
			uni.showLoading({
				title: '加载中...',
				mask: true // 是否显示遮罩
			});
			// setTimeout(() => {
			// 	this.isLoading = true
			// 	uni.hideLoading(); // 异步操作完成后关闭loading
			// }, 3000);
		},
		methods: {
			callMe() {
				uni.makePhoneCall({
					phoneNumber: '15923722588' //仅为示例
				});
			},
			applyButtonClicked() {
				if (!this.userLogin()) {
					return
				}
				uni.navigateTo({
					url: '/pages/PageApply'
				})
			},
			checkButtonClicked() {
				if (!this.userLogin()) {
					return
				}
				uni.navigateTo({
					url: '/pages/PageApplyHistoryList'
				})
			},
			userLogin() {
				if (!this.userId && process.env.NODE_ENV != 'development') {
					uni.showToast({
						title: '请登录渝快办'
					})
					return false
				}
				return true
			},
			getToken() {
				let self = this
				request.post('/lsydfk/ykb/get/ykbInfo/prod', {
					"ykbFlag": "0"
				}).then(res => {
					// let data = JSON.parse(res)
					console.log(res, 'getToken')
					if (res.code == 0) {
						self.jsapiToken = res.data.jsapiToken
						self.refreshToken = res.data.refreshToken
						uni.setStorageSync('jsapiToken', self.jsapiToken)
						self.getAuthCodeFn()

					}
				}).catch(err => {

				})
			},

			jsSDKReady(callBack) {
				if (window.AlipayJSBridge) {
					callBack && callBack()
				} else {
					document.addEventListener('AlipayJSBridgeReady', callBack, false)
				}
			},
			//获取渝快办用户ID 弃用
			// getYkbUserID() {
			// 	let self = this
			// 	AlipayJSBridge.call('getuseridApi', {
			// 		jsapiToken: this.jsapiToken,
			// 	}, (res) => {
			// 		console.log('ykbUserID=', res)
			// 		if (res.code == 0) {
			// 			console.log('ykbUserID=', res)
			// 			self.userId = res.userId
			// 			self.userType = res.userType
			// 			uni.setStorageSync('userId', self.userId)
			// 			uni.setStorageSync('userType', self.userType)
			// 			// self.getAuthCode(self.userId)
			// 			// self.getImage()
			// 			// self.getscanQRCode()
			// 			// self.saveImage()
			// 		} else if (res.code == 100) {

			// 		}
			// 	})
			// },
			//获取authCode
			getAuthCodeFn() {
				let self = this
				AlipayJSBridge.call('getYkbAuthCode', {
					jsapiToken: this.jsapiToken,
					appId: 'dd836128-6bf1-41cf-80e3-c4671ec3e36d',
					forceScopes: ['ykb_user_info']
				}, (res) => {
					if (res.code == 0) {
						console.log('新getAuthCodeFn', res)
						self.authCode = res.data.authCode
						if (self.authCode) {
							self.getUserInfo(self.authCode)
							// 调文号接口
							// self.getYkbUserInfo()
							// self.getYkbUserEnterprise()
						}
					}
				})
			},
			// 获取用户信息  文号
			getUserInfo(authCode) {
				let self = this
				request.post('/lsydfk/ykb/get/ykbInfo/prod', {
					"ykbFlag": "2",
					'authCode': self.authCode
				}).then(res => {
					console.log('userInfo=', res.userInfo)
					this.isLoading = true
					uni.hideLoading();
					self.userId = res.userInfo.id
					self.userType = res.userInfo.userType
					uni.setStorageSync('userInfo', JSON.stringify(res.userInfo))
					uni.setStorageSync('userType', res.userInfo.userType)
					uni.setStorageSync('appToken', res.appToken)

				}).catch(err => {
					console.log(err, 'err')
				})
			},
			// 打开图片获取页面
			getImage() {
				let self = this
				AlipayJSBridge.call('getImageBase64', {
					'jsapiToken': self.jsapiToken,
					'imageType': '["album","camera"]',
					'sizeType': 'original'
				}, (res) => {
					console.log('APP返回的图片=', res)
					if (res.code == 0) {
						let temp = res.data[0]
						self.listImage.push(temp)
						console.log('已有的图片=', self.listImage)
					}
				})
			},
			getBase64Image(val) {
				return "data:image/jpg;base64," + val
			},
			// APP保存图片 等待后端接口传递参数
			saveImage() {
				let self = this
				AlipayJSBridge.call('saveImageToAIbum', {
					'jsapiToken': self.jsapiToken,
					'url': 'https://image.baidu.com/search/detail?ct=503316480&z=&tn=baiduimagedetail&ipn=d&word=%E5%8A%A8%E7%89%A9&step_word=&lid=9937687810143666893&ie=utf-8&in=&cl=2&lm=-1&st=-1&hd=undefined&latest=undefined&copyright=undefined&cs=2308777326,4174452281&os=1848747309,483433038&simid=2625231,693204599&pn=0&rn=1&di=46137345&ln=1914&fr=&fmq=1712652351538_R&ic=0&s=undefined&se=&sme=&tab=0&width=&height=&face=undefined&is=0,0&istype=2&ist=&jit=&bdtype=11&spn=0&pi=0&gsm=1e&objurl=https%3A%2F%2Fww3.sinaimg.cn%2Fmw690%2F003w6C4Mly1hoj63d2y4rj60u01hc4ah02.jpg&rpstart=0&rpnum=0&adpicid=0&nojc=undefined',
					'base64': "://image.baidu.com/search/detail?ct=503316480&z=&tn=baiduimagedetail&ipn=d&word=%E5%8A%A8%E7%89%A9&step_word=&lid=9937687810143666893&ie=utf-8&in=&cl=2&lm=-1&st=-1&hd=undefined&latest=undefined&copyright=undefined&cs=2308777326,4174452281&os=1848747309,483433038&simid=2625231,693204599&pn=0&rn=1&di=46137345&ln=1914&fr=&fmq=1712652351538_R&ic=0&s=undefined&se=&sme=&tab=0&width=&height=&face=undefined&is=0,0&istype=2&ist=&jit=&bdtype=11&spn=0&pi=0&gsm=1e&objurl=https%3A%2F%2Fww3.sinaimg.cn%2Fmw690%2F003w6C4Mly1hoj63d2y4rj60u01hc4ah02.jpg&rpstart=0&rpnum=0&adpicid=0&nojc=undefined",
				}, (res) => {
					console.log('APP保存的图片=base64', res)
					if (res.code == 0) {
						let temp = res.data[0]
						// self.listImage.push(temp)
						console.log('已有的图片=base64', temp)
					}
				})
			},
			// app拉取扫一扫
			getscanQRCode() {
				let self = this
				AlipayJSBridge.call('scanQRCode', {
					'jsapiToken': self.jsapiToken,
				}, (res) => {
					console.log('拉取扫一扫APP=', res)
				})
			},
			// loginYkb() {
			// 	AlipayJSBridge.call('login', {
			// 		jsapiToken: this.jsapiToken,
			// 	}, (res) => {
			// 		console.log('ykbLogin=', res)
			// 		if (res.result) {
			// 			this.getYkbUserID()
			// 		} else {
			// 			uni.showToast({
			// 				title: '请登录渝快办再进行操作'
			// 			})
			// 		}
			// 	})
			// },
			//渝快办用户授权
			// getYkbUserAuth(val) {
			// 	AlipayJSBridge.call('userAuth', {
			// 		reqCode: this.reqCode
			// 	}, (res) => {
			// 		console.log('userAuth=', res)
			// 	})
			// },
			// getAuthCode(userId) {
			// 	let self = this
			// 	request.get(`/lsydfk/ykb/get/authCode/reqCode?userId=${userId}`).then(res => {
			// 		let data = JSON.parse(res)
			// 		console.log(data, '获取AuthCode')
			// 		self.authCode = data.data.authCode ? data.data.authCode : ''
			// 		self.reqCode = data.data.reqCode ? data.data.reqCode : ''
			// 		if (self.authCode) {
			// 			self.getYkbUserInfo()
			// 			self.getYkbUserEnterprise()
			// 		}
			// 		if (self.reqCode) {
			// 			self.getYkbUserAuth()
			// 		}
			// 	}).catch(err => {

			// 	})
			// },
			// 获取用户账号信息（脱敏）（弃用）
			// getYkbUserInfo() {
			// 	try {
			// 		let self = this
			// 		AlipayJSBridge.call('getYkbUserInfoApi', {
			// 			jsapiToken: this.jsapiToken,
			// 			authCode: this.authCode,
			// 		}, (res) => {
			// 			console.log('获取用户账号信息（脱敏）', res)
			// 			if (res.code == 0) {
			// 				console.log('UserInfo=', res)
			// 				self.userInfo = res.data
			// 				uni.setStorageSync("userInfo", JSON.stringify(self.userInfo))
			// 			} else if (res.code == 100) {
			// 				uni.showToast({
			// 					title: '请登录渝快办再进行操作'
			// 				})
			// 			}
			// 		})
			// 	} catch (e) {
			// 		console.log(e, 'eee')
			// 	}

			// },
			// 当前用户所有企业（弃用）
			// getYkbUserEnterprise() {
			// 	let self = this
			// 	AlipayJSBridge.call('getYkbUserEnterpriseApi', {
			// 		jsapiToken: this.jsapiToken,
			// 		authCode: this.authCode,
			// 	}, (res) => {
			// 		console.log('userCurrentQy=', res)
			// 		if (res.code == 0) {
			// 			self.userCurrentQy = res.data.select
			// 			self.userAllQy = res.data.all
			// 			console.log('当前用户企业信息', self.userCurrentQy)
			// 			uni.setStorageSync("userCurrentQy", JSON.stringify(self.userCurrentQy))
			// 			uni.setStorageSync("userAllQy", JSON.stringify(self.userAllQy))
			// 		} else if (res.code == 100) {
			// 			uni.showToast({
			// 				title: '请登录渝快办再进行操作'
			// 			})
			// 		}
			// 	})
			// },
		},
	}
</script>

<style lang="scss" scoped>
	uni-page-body {
		height: 100%;
	}

	.bottom-box {
		position: absolute;
		bottom: 50px;
		text-align: center;
		width: 100%;
	}

	.home-box {
		background-image: url(../static/image/home-bg.jpg);
		background-repeat: no-repeat;
		background-size: 100% 100%;
		width: 100%;
		height: 100%;
	}

	.home-header {
		padding-top: 48px;
		margin-left: 20px;
		// width: 396rpx;
		height: 144rpx;
		color: #fff;

		&-title1 {
			font-size: 40rpx;
			margin-top: 30rpx;
		}

		&-title2 {
			font-size: 50rpx;
			margin-top: 30rpx;
		}
	}

	.home-container {
		// margin: 209px 18px 0px 18px;
		// height: 647rpx;
		padding: 15px 10px;
		// width: 314px;
		// height: 180px;
		background: rgba(219, 239, 247, 0.8);
		border-radius: 8px 8px 8px 8px;
		display: flex;
		align-items: center;
		flex-direction: column;
		justify-content: space-around;
		position: absolute;
		bottom: 25%;
		left: 18px;
		right: 18px;
	}

	.home-sub {
		display: flex;
		// flex-direction: column;
		align-items: center;
		justify-content: flex-start;
		// height: 277rpx;
		margin-top: 10rpx;
		// width: 286px;
		width: 100%;
		height: 70px;
		background: #FFFFFF;
		border-radius: 8px 8px 8px 8px;
		opacity: 0.96;

		.home-sub-image {
			width: 71px;
			height: 71px;
		}
	}

	.home-sub-status {}

	.home-sub-item {
		// margin-left: 313rpx;
		flex: 1;
		display: flex;
		justify-content: space-between;
		align-items: center;
		padding-right: 10px;

		&-title {
			font-weight: 500;
			font-size: 18px;
			color: #1A1A1A;
			// margin-bottom: 30rpx;

		}

		&-button {
			font-weight: 400;
			font-size: 12px;
			color: #1E92F0;
			line-height: 20px;
			text-align: center;

		}
	}
</style>